package com.anxin.act.listener;

import com.anxin.common.utils.StringUtils;
import com.anxin.common.utils.sys.DeptUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.sys.system.entity.SysDept;
import com.anxin.sys.system.entity.SysUser;
import org.activiti.engine.delegate.DelegateExecution;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;


@Configuration
public class UserListener {

    /**
     * 例子 ${userListener.applyRegional(execution,'北京,上海')}
     * 判断流程申请人是否在某个地域
     * @param execution 流程运行对象
     * @param regional 地域，多个逗号分隔
     * @return
     */
    public boolean applyRegional(DelegateExecution execution,String regional){
        // 申请人ID
        String applyUserName = execution.getVariable("applyUserName", String.class);
        // 获取申请人
        SysUser sysUser = UserUtils.getUserByUserName(applyUserName);
        // 获取申请人部门
        SysDept sysDept = DeptUtils.get(sysUser.getDeptId());

        List<String> regionalList = Arrays.asList(regional.split(","));
        if (sysDept != null && StringUtils.isNotBlank(sysDept.getAddress())){
            String[] deptAddress = sysDept.getAddress().split(",");
            for (String address : deptAddress) {
                if (regionalList.contains(address)){
                    return true;
                }
            }

        }
        return false;
    }


}
